* Firm organization with multiple establishments
* Section V: Table VII, Appendix Tables D.3, D.6, D.8, D.15-D.16, D.18-D.24 
* Middle panel: Headquarters

clear all 
set matsize 2000
set more off

capture log close
log using log/20b_train_layer-firm_hq.log, replace

/*
Contents: Results for headquarters

	Table VII: Lower travel times affect all units of ME firms
	Table D.3: summary statistics

	Robustness to alternative weighting, matching, statistical inference
		Table D.6: Table VII unweighted
		Table D.8: Table VII without matching

	Robustness to alternative definitions of the main variables
		Table D.15: Table VII with additional outcome variables (firms with at least two establishments)
				log total number of employees; 
				average log wages of all employees;
				managerial span, defined as ratio of the number of employees over number of employees in the top layer (as in Friedrich 2020); 
				share of managerial occupations in wage sum in percent, where managerial occupations are determined by layer;
				ed. 0: education of non-managerial employees;
				ed. all: education of all employees;
				ex. 0: average log experience (= # of days in establishment/HQ) of non-managerial employees;
				ex. all: average log experience of all employees
		Table D.16: Table VII with stricter and broader treatment definition
		Table D.18: Table VII with Bundesland x year FE

	Robustness to alternative sample restrictions
		Table D.19: Table VII without route HH-Berlin
		Table D.20: Table VII without largest units per county
		Table D.21: Table VII without stations on route
		Table D.22: Table VII w/o moving establishments
		Table D.23: Table VII with only always connected stations	

	Helping cost channel
		Table D.24: sample split by predictability	
*/
		
use $data/train_analysis_firm.dta, clear
desc

//	For D.18
cap drop bula
gen bula = int(hq_kreis / 1000)
gen double bula_year = bula * 10000 + jahr

//	For D.23
merge m:1 ao_kreis using data/AlwaysICE_ao.dta, keepusing(stetsICE)
drop _merge

replace stetsICE = 1 if ao_kreis == 2000 // Berlin
rename stetsICE ao_stetsICE

merge m:1 hq_kreis using data/AlwaysICE.dta, keepusing(stetsICE)
drop _merge

rename stetsICE hq_stetsICE
replace hq_stetsICE = 1 if hq_kreis == 2000

rename ao_stetsICE stetsICE
gen aux_ICE = (stetsICE == 1) if hauptbet == 0 & d_traindata == 1
bys untid: egen est_stetsICE = min(aux_ICE)

********************************************************************************
***	HEADQUARTERS	************************************************************
********************************************************************************

********************************************************************************
***	BASELINE REGRESSION
*** TABLE VII, full sample, Table D.3, D.6

{
preserve
keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)
	eststo vceunw`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vceunw`j'
		qui estadd scalar count_kreis = e(K2): vceunw`j'

	local j = `j' + 1
}

qui reghdfe ln_prdt_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)

********************************************************************************
*** Table D.3: Descriptive statistics

foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet firm_faster1{
	tabstat `outcome' if e(sample), by(jahr) s(n mean sd p5 p10 q p90 p95)
}

********************************************************************************
*** TABLE VII: Lower travel times affect all units of ME firms, middle left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
***	Table D.6: Table VII without weights, middle left
esttab vceunw1 vceunw2 vceunw3 vceunw4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceunw1 vceunw2 vceunw3 vceunw4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
*** Not reported: additional outcome variables (only reported for firms with two establishments)
local j = 1
foreach outcome in ln_empl_bet avg_lnw_bet ln_span_bet_bf shr_mgmt_w_bet avg_d_educ_bot_bet avg_d_educ_bet avg_tage_bet_bot_bet avg_tage_bet_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcew`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): vcew`j'
		qui estadd scalar count_kreis = e(K2): vcew`j'
	local j = `j' + 1
}

esttab vcew1 vcew2 vcew3 vcew4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcew5 vcew6 vcew7 vcew8, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	BASELINE REGRESSION FOR FIRMS WITH MORE THAN ONE ESTABLISHMENT
*** Table VII, Table D.6, D.15

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vceunw`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vceunw`j'
		qui estadd scalar count_kreis = e(K2): vceunw`j'

	local j = `j' + 1
}

********************************************************************************
***	TABLE VII: Lower travel times affect all units of ME firm, middle right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
***	Table D.6: Table VII without weights, middle right
esttab vceunw1 vceunw2 vceunw3 vceunw4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceunw1 vceunw2 vceunw3 vceunw4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
*** Additional outcome variables
local j = 1
foreach outcome in ln_empl_bet avg_lnw_bet ln_span_bet_bf shr_mgmt_w_bet avg_d_educ_bot_bet avg_d_educ_bet avg_tage_bet_bot_bet avg_tage_bet_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcew`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): vcew`j'
		qui estadd scalar count_kreis = e(K2): vcew`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.15: Additional outcome variables, top panel
esttab vcew1 vcew2 vcew3 vcew4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcew5 vcew6 vcew7 vcew8, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
		
restore
}

********************************************************************************
***	Table D.8: Table VII without matching

**	All firms
{
preserve

keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

//	Make sure that all counties in sample contain treated and control observations
egen group_strata = group(hq_kreis  jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0) 

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo kreunw`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): kreunw`j'
		qui estadd scalar count_kreis = e(K2): kreunw`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.8 middle left
esttab kreunw1 kreunw2 kreunw3 kreunw4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab kreunw1 kreunw2 kreunw3 kreunw4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

***	Firms with at least two establishments
{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

//	Make sure that all counties in sample contain treated and control observations
egen group_strata = group(hq_kreis jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo kreunw`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): kreunw`j'
		qui estadd scalar count_kreis = e(K2): kreunw`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.8, middle right
esttab kreunw1 kreunw2 kreunw3 kreunw4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab kreunw1 kreunw2 kreunw3 kreunw4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Stricter definition of treatment
***	Unreported, full sample

{
preserve
keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1_direct == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1_direct == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1_direct if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1_direct == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1_direct == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1_direct if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1_direct == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1_direct == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1_direct == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1_direct == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1_direct if firm_ever_faster1_direct == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1_direct if firm_ever_faster1_direct == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1_direct == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1_direct == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1_direct if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1_direct if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
***	Unreported: stricter treatment definition, full sample
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Stricter definition of treatment
***	Table D.16, firms with at least two establishments

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
tabstat empl_2000 if flg_est == 1, s(n mean sd p5 q p95)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1_direct == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1_direct == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1_direct if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1_direct == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1_direct == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1_direct if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1_direct == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1_direct == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1_direct == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1_direct == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1_direct if firm_ever_faster1_direct == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1_direct if firm_ever_faster1_direct == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1_direct == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1_direct == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1_direct if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1_direct if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
***	Table D.16: Stricter treatment definition, firms with at least two establishments
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Bundesland-year FE
*** Table D.18, full sample

{
preserve
keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr bula_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr bula_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.18, middle left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	Bundesland-year FE
*** Table D.18, firms with more than one establishment

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr bula_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr bula_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.18, middle right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
*** DROP CONNECTION HH-BERLIN
*** Table D.19, full sample

{
preserve
keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1_rob == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1_rob == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1_rob if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1_rob == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1_rob == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1_rob if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1_rob == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1_rob == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1_rob == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1_rob == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1_rob if firm_ever_faster1_rob == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1_rob if firm_ever_faster1_rob == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1_rob == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1_rob == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1_rob if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1_rob if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.19, middle left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
*** DROP CONNECTION HH-BERLIN
*** Table D.19, firms with more than one establishment

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1_rob == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1_rob == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1_rob if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1_rob == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1_rob == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1_rob if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1_rob == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1_rob == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1_rob == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1_rob == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1_rob if firm_ever_faster1_rob == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1_rob if firm_ever_faster1_rob == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1_rob == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1_rob == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1_rob if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1_rob if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.19, firms with more than one establishment
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
*** Exclude HQs that are the largest in county
*** Table D.18, full sample

{
preserve
keep if hauptbet == 1 & e_traindata == 1

bys hq_kreis jahr: egen max_est_cty = max(empl_bet)
drop if empl_bet == max_est_cty

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
***	Table D.20, bottom left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Exclude HQs that are the largest in county
*** Table D.18, firms with more than one establishment

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

bys hq_kreis jahr: egen max_est_cty = max(empl_bet)
drop if empl_bet == max_est_cty

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
***	Table D.20, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Exclude HQs located along the high-speed routes
***	Table D.21, full sample

{
preserve
keep if hauptbet == 1 & e_traindata == 1

drop if inlist(hq_kreis, 6533, 7143, 5382, 9161, 15091, 12070, 13076)

// 6533 - Limburg Sued, 7143 - Montabaur, 5382 - Siegburg/Bonn,
// 9161 - Ingolstadt
// 15091 - Lutherstadt Wittenberg
// 12070 - Wittenberge, 13076 - Ludwigslust

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
***	Table D.21, bottom left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Exclude HQs located along the high-speed routes
***	Table D.19, firms with more than one establishment

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

drop if inlist(hq_kreis, 6533, 7143, 5382, 9161, 15091, 12070, 13076)

// 6533 - Limburg Sued, 7143 - Montabaur, 5382 - Siegburg/Bonn,
// 9161 - Ingolstadt
// 15091 - Lutherstadt Wittenberg
// 12070 - Wittenberge, 13076 - Ludwigslust

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
***	Table D.21, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Drop moving establishments
*** Table D.22, full sample

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & issue_est == 0

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.22, middle left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
*** Drop moving establishments
*** Table D.22, firms with more than one establishment

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & issue_est == 0 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.22, middle right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	Station connected to ICE network in all years
*** Table D.23, full sample

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & hq_stetsICE == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.23, middle left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	Station connected to ICE network in all years
*** Table D.23, firms with more than one establishment

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & hq_stetsICE == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.23, middle right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	By predictability / problem arrival rate
*** Table D.24, full sample

{
//	Below-median problem arrival rate/ above-median predictability
preserve

keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

cap drop flg_br1
egen flg_br1 = tag(br1)
count if flg_br1 == 1

qui sum mean_Dp_R_25 if flg_br1 == 1 & mean_Dp_R_25 != 0, d
cap drop touse
qui gen touse = (mean_Dp_R_25 <= r(p50)) // & mean_Dp_R_25 != 0)
count if touse == 1
count if touse == 1 & flg_est == 1
keep if touse == 1
drop touse

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.24, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
		
restore	
}

//	Above-median arrival rate/below-median predictability
{
preserve
keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

cap drop flg_br1
egen flg_br1 = tag(br1)
count if flg_br1 == 1

qui sum mean_Dp_R_25 if flg_br1 == 1 & mean_Dp_R_25 != 0, d
cap drop touse
qui gen touse = (mean_Dp_R_25 > r(p50)) // & mean_Dp_R_25 != 0)
count if touse == 1
count if touse == 1 & flg_est == 1
keep if touse == 1
drop touse

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

//	define size groups
forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
egen flg_strata = tag(perc_empl hq_kreis jahr)

bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.24, bottom left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
restore	
}

log close
